﻿server_tips_view = {
    pic_width = 93;
    pic_height = 108;
    image_type_logo_pic = [[ONEIMAGE:newui_mainframe\\mini_mode.png?259,485,321,501;Stretch;0;]];

    wantplay_pic_width = 48;
    wantplay_pic_height = 48;
}

function server_tips_view.init()
    ierd_tgp.tpf_ui.Require("poptips\\model.lua");
    ierd_tgp.tpf_ui.Require("poptips\\view.lua");
end

function server_tips_view.open_link(uilib, ctr_id)
    local max_loop = 5;
    local loop = 0;
    local opts = 0;
    local cur_id = ctr_id;
    while opts == 0 and loop < max_loop do
        cur_id = ui_utility.get_ctrl_parent_id(cur_id);
        opts = poptips_view:get_ctr_data(cur_id);
        loop = loop + 1;
    end

    if opts == 0 then return end

    if opts.link and opts.link ~= "" then
        base_utility.log("[server_tips_view] open_link is " .. tostring(opts.link));
        if wegame_mainframe_view.is_closed == false then
            base_utility.log("[server_tips_view] tgp is open status");
            web_utility.open_link(opts.link);
            poptips_view:_close(uilib, cur_id);
        else
            base_utility.log("[server_tips_view] tgp is closed status");
            if wegame_mainframe_view.show(false) then
                event_center.send_event(mainframe_event.MAINFRAME_WND_SHOWN, "");
                server_tips_control.link = opts.link;
                server_tips_control.opts = opts;
                server_tips_control.view_uilib = uilib;
                server_tips_control.view_cur_id = cur_id;
                event_center.add_listener(web_register_event.REGISTER_SUCCESS_EVENT,
                "server_tips_control.handle_register_event");
            end
        end
        poptips_model.tips_mgr:SendPopTipsOP(
                (opts.tip_id or 0), ierd_tgp.tips_mgr.KIND_TIP_CLICK, 0);
        return;
    end
    
end

function server_tips_view.cancelQblinkRestartListener()
    event_center.remove_listener(web_register_event.REGISTER_SUCCESS_EVENT,
                "server_tips_control.handle_register_event");
                base_utility.log(
                "[server_tips_view] close server tips and cancel listener on Qblink");
end

function server_tips_view.toStr(str)
    local utfs, utfh = U8toCUIStr(str);
    return utfs;
end

function server_tips_view.layout(win_id, opts)
    local uilib = ui_utility.uilib();
    local margin = 10;
    local add = 18;
    local tOffset, tMaxWidth, lineWidth = 0, 0, 2;
    local hasPic = opts.icon and opts.icon ~= "";
    local hasCaptionIcon = opts.hide_caption_icon;
    
    if opts.type ~= nil and opts.type == 0 then
        local pic_con_id = ui_utility.get_ctrl_id_by_path(win_id, "Content\\Pic");
        local pic_id = ui_utility.get_ctrl_id_by_path(win_id, "Content\\Pic\\Button_1");
        local mask_id = ui_utility.get_ctrl_id_by_path(win_id,"mask");
        ui_utility.setproperty_by_id(mask_id, "Visible", "false");
        local container_id = ui_utility.get_ctrl_id_by_path(win_id,"Content");
        ui_utility.setproperty_by_id(container_id, 'Visible', "true");

        if hasPic then
            ui_utility.setproperty_by_id(pic_id, 'Width', tostring(server_tips_view.pic_width));
            ui_utility.setproperty_by_id(pic_id, 'Height', tostring(server_tips_view.pic_height));
            SetBackground_ID(uilib, pic_id, opts.icon);
            tOffset = margin * 2 + server_tips_view.pic_width + lineWidth;--20+93+2 = 115
            tMaxWidth = opts.width - tOffset - margin - add;
        else
            tOffset = margin + add;
            tMaxWidth = opts.width - 2 * (margin + add);

            
        end
        ui_utility.setproperty_by_id(pic_con_id, 'XOffset', tostring(margin));
        ui_utility.setproperty_by_id(pic_con_id, 'Visible', tostring(hasPic));
            -- 内容文字调整
        if opts.descTitle and opts.descTitle ~= "" then
            -- local color = "0xffffff";
            local interface = "Tenio::IRichTextBox";
            local title_con = opts.descTitle;
            local title_id = ui_utility.get_ctrl_id_by_path(win_id,
            "Content\\link_title");
            local pTitleControl = uilib:GetControlPtr(title_id);
            if pTitleControl  == nil then 
                base_utility.log("[server_tips_view] link_title not found");
            end
            local pConTitle = tolua.cast(pTitleControl, interface);

            

            ui_utility.setproperty_by_id(title_id, 'XOffset',
            tostring(tOffset));
            
            ui_utility.setproperty_by_id(title_id, 'Width',
            tostring(tMaxWidth));
            if hasPic == false then
                base_utility.log("[server_tip] haspic is false");
                ui_utility.setproperty_by_id(title_id, 'ElemAlign', "center");
            else
                base_utility.log("[server_tip] haspic is true"); 
                ui_utility.setproperty_by_id(title_id, 'ElemAlign', "left");
            end
            pConTitle:Clear();
            pConTitle:PushBackHtmlString(
            server_tips_view.toStr(title_con));
        end
        -- 正文内容
        if opts.desc and opts.desc ~= "" then
            -- local color = "0xb5b9c5";
            local interface = "Tenio::IRichTextBox";
            local text_con = opts.desc;
            local text_id = ui_utility.get_ctrl_id_by_path(win_id, "Content\\link_content");
            local pTextControl = uilib:GetControlPtr(text_id);
            if pTextControl  == nil then 
                base_utility.log("[server_tips_view] link_content ptr not found");
            end
            local pConText = tolua.cast(pTextControl, interface);
            ui_utility.setproperty_by_id(text_id, 'XOffset',
            tostring(tOffset));
            ui_utility.setproperty_by_id(text_id, 'Width',
            tostring(tMaxWidth));
            pConText:Clear();

            pConText:PushBackHtmlString(
            server_tips_view.toStr(text_con));
        end

        -- 配置链接和跳转按钮
        if opts.link and opts.link ~= "" then
            local btnWidth = 84;
            local btnMarginLeft = margin;
            local click_btn_id = ui_utility.get_ctrl_id_by_path(win_id,
            "Content\\click_btn");

            ui_utility.setproperty_by_id(click_btn_id, 'Visible', 'true');
            ui_utility.setproperty_by_id(click_btn_id, 'XOffset',
            tostring(tonumber(opts.width) - btnWidth - btnMarginLeft));

            -- 增加切换按钮文本的功能, "button_text"字段
            local info = base_utility.json_to_table(opts.link);
            if info and info.button_text and info.button_text ~="" then
                ui_utility.setproperty_by_id(click_btn_id, 'Caption', 
                  info.button_text);
            end
        end
    
        -- 标题栏
        local title_bar_id = ui_utility.get_ctrl_id_by_path(win_id,
        "title_bar");
        ui_utility.setproperty_by_id(title_bar_id, 'Width', opts.width);
        if hasCaptionIcon == true then
          local logo_icon_id = ui_utility.get_ctrl_id_by_path(win_id,"title_bar\\logo_icon");
              ui_utility.setproperty_by_id(logo_icon_id, 'Visible', "false");
        end
        
        -- 关闭按钮
        local closeWidth = 17;
        local closeMarginLeft = margin;
        local close_btn_id = ui_utility.get_ctrl_id_by_path(win_id,
        "title_bar\\close_btn");
        ui_utility.setproperty_by_id(close_btn_id, 'XOffset',
        tostring(tonumber(opts.width) - closeWidth - closeMarginLeft));

    elseif opts.type ~= nil and opts.type == 4 then -- 全图模式
        
        local container_id = ui_utility.get_ctrl_id_by_path(win_id,"Content");
        ui_utility.setproperty_by_id(container_id, 'Visible', "false");
        local logo_icon_id = ui_utility.get_ctrl_id_by_path(win_id,"title_bar\\logo_icon");
        ui_utility.setproperty_by_id(logo_icon_id, 'Background',
            server_tips_view.image_type_logo_pic);
        local mask_id = ui_utility.get_ctrl_id_by_path(win_id,"mask");
        ui_utility.setproperty_by_id(mask_id, "Visible", "true");
        ui_utility.setproperty_by_id(mask_id, 'OnClick', "server_tips_view.open_link");
        ui_utility.setproperty_by_id(win_id,"Background",opts.icon);

        -- 标题栏
        local title_bar_id = ui_utility.get_ctrl_id_by_path(win_id,
        "title_bar");
        ui_utility.setproperty_by_id(title_bar_id, 'Width', opts.width);

        -- 关闭按钮
        local closeWidth = 21;
        local closeMarginLeft = margin;
        local close_btn_id = ui_utility.get_ctrl_id_by_path(win_id,
        "title_bar\\close_btn");
        ui_utility.setproperty_by_id(close_btn_id, 'XOffset',
        tostring(tonumber(opts.width) - closeWidth - closeMarginLeft));
        
    end
   
end;

-- "想玩"Tips推送功能布局
function server_tips_view.layout_wantplay(win_id, opts)
    -- 显示图片
    local dw_pic_id = ui_utility.get_ctrl_id_by_path(
                                            win_id,
                                            "content_main\\icon_wantplay");
    -- 设置图片内容
    ui_utility.setproperty_by_id(dw_pic_id, "Background", opts.icon);

    -- 显示展示内容 -- 标题
    if nil ~= opts.descTitle and "" ~= opts.descTitle then
        local int_richtextbox = "Tenio::IRichTextBox";
        local p_titlecontent = ui_utility.get_ctrl_ptr_by_path(
                                        win_id,
                                        "content_main\\txt_linktitle");
        local p_titlecontent_rich = tolua.cast(p_titlecontent, int_richtextbox);
        p_titlecontent_rich:Clear();
        p_titlecontent_rich:PushBackHtmlString(server_tips_view.toStr(opts.descTitle));
    end;

    -- 显示展示内容 -- 正文
    if nil ~= opts.desc and "" ~= opts.desc then
        local int_richtextbox = "Tenio::IRichTextBox";
        local p_content = ui_utility.get_ctrl_ptr_by_path(
                                        win_id,
                                        "content_main\\txt_linkcontent");
        local p_content_rich = tolua.cast(p_content, int_richtextbox);
        p_content_rich:Clear();
        p_content_rich:PushBackHtmlString(server_tips_view.toStr(opts.desc));
    end;
end;

function server_tips_view.CommentInviteLayout(win_id, opts)
    local uilib = ui_utility.uilib();

    -- 标题
    if opts.descTitle and opts.descTitle ~= "" then
        local interface = "Tenio::IRichTextBox";
        local title_con = opts.descTitle;
        local title_id = ui_utility.get_ctrl_id_by_path(win_id,
        "Content\\link_title");
        local title_control = uilib:GetControlPtr(title_id);
        local con_title = tolua.cast(title_control, interface);

        ui_utility.setproperty_by_id(title_id, 'ElemAlign', "center");

        con_title:Clear();
        con_title:PushBackHtmlString(server_tips_view.toStr(title_con));
    end

    -- 正文内容
    if opts.desc and opts.desc ~= "" then
        local interface = "Tenio::IRichTextBox";
        local text_con = opts.desc;
        local text_id = ui_utility.get_ctrl_id_by_path(win_id, "Content\\link_content");
        local text_control = uilib:GetControlPtr(text_id);
        local con_text = tolua.cast(text_control, interface);

        con_text:Clear();
        con_text:PushBackHtmlString(server_tips_view.toStr(text_con));
    end
end;

server_tips_view.init();
